package com.example.partner.activity.home;
import retrofit2.Retrofit;
import retrofit2.Call;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.Callback;
import retrofit2.Response;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import com.example.partner.R;
import com.example.partner.common.BaseResponse;
import com.example.partner.common.ConfigConstant;
import com.example.partner.service.UserService;
import com.example.partner.model.request.UserRegisterRequest;

public class RegisterActivity extends AppCompatActivity {

    final String TAG = "RegisterActivity";
    private EditText etUserAccount;
    private EditText etUserPassword;
    private EditText etCheckPassword;
    private Button btnRegister;
    private UserService userService;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        // 初始化UI组件
        etUserAccount = findViewById(R.id.etUserAccount);
        etUserPassword = findViewById(R.id.etUserPassword);
        etCheckPassword = findViewById(R.id.etCheckPassword);
        btnRegister = findViewById(R.id.btnRegister);
        // 初始化Retrofit
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(ConfigConstant.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        userService = retrofit.create(UserService.class);
        btnRegister.setOnClickListener(view -> registerUser());
    }

    private void registerUser() {
        String userAccount = etUserAccount.getText().toString();
        String userPassword = etUserPassword.getText().toString();
        String checkPassword = etCheckPassword.getText().toString();

        UserRegisterRequest request = new UserRegisterRequest();
        request.setUserAccount(userAccount);
        request.setUserPassword(userPassword);
        request.setCheckPassword(checkPassword);

        userService.userRegister(request).enqueue(new Callback<BaseResponse<Long>>() {
            @Override
            public void onResponse(Call<BaseResponse<Long>> call, Response<BaseResponse<Long>> response) {
                BaseResponse<Long> responseBody = response.body();
                String message = responseBody.message;
                Long userId = responseBody.data;
                if (response.isSuccessful()) {
                    // 处理成功的响应
                    Toast.makeText(RegisterActivity.this,userId+"注册成功！",Toast.LENGTH_SHORT);
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    // 基于responseBody进行进一步处理
                } else {
                    Toast.makeText(RegisterActivity.this,message,Toast.LENGTH_SHORT);

                    // 处理错误情况
                }
            }

            @Override
            public void onFailure(Call<BaseResponse<Long>> call, Throwable t) {
                // 网络错误处理
                Log.e(TAG, "post回调失败：" + t.getMessage() + "," + t.toString());
                Toast.makeText(RegisterActivity.this, "post回调失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
